package com.gobo.jinghuimall.orders.intercptor;


import com.alibaba.fastjson.JSONObject;
import com.gobo.common.constant.AuthServerConstant;
import com.gobo.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class OrderInterceptor implements HandlerInterceptor {

    public static final ThreadLocal<MemberResponseVo> userThreadLocal = new ThreadLocal<>();

    /**
     * 必须登录后才能进入订单
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object a = request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (a!=null) {
                MemberResponseVo attribute = JSONObject.toJavaObject(JSONObject.parseObject(a.toString()), MemberResponseVo.class);
                userThreadLocal.set(attribute);
            return true;
        } else {
            String requestURI = request.getRequestURI();
            int index = requestURI.indexOf("/order/status/");
            if (index!=-1){
                return true;
            }
            request.getSession().setAttribute("msg", "请先登录");
            response.sendRedirect("http://auth.jinghuimall.com/login.html");
            return false;
        }
    }
}
